package com.iafme.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.zhxu.bs.BeanSearcher;
import cn.zhxu.bs.operator.Equal;
import cn.zhxu.bs.util.MapUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iafme.dto.req.CertificateOrderReqDTO;
import com.iafme.dto.res.CertificateInfoResDTO;
import com.iafme.dto.res.CertificateOrderResDTO;
import com.iafme.model.CertificateOrder;
import com.iafme.properties.ScanPayReq;
import com.iafme.service.CertificateInfoService;
import com.iafme.service.CertificateOrderService;
import com.systemframework.constants.Constants;
import com.systemframework.sys.dto.request.common.PageResponseDTO;
import com.systemframework.util.SearchUtl;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author summer
 * @version 1.0
 * @date 2025/3/25 21:37
 * 证书订单
 */
@RestController
@RequestMapping(value = "/certificateOrder")
@AllArgsConstructor
public class CertificateOrderController {


    private final BeanSearcher beanSearcher;

    private final CertificateOrderService orderService;

    @PostMapping(value = "/lists")
    public Page<CertificateOrderResDTO> lists(@RequestBody PageResponseDTO dto) {
        Map<String, Object> build = MapUtils.builder().page(dto.getPageNum() - 1, dto.getPageSize()).field(CertificateOrderResDTO::getMemberId, StpUtil.getLoginIdAsInt()).op(Equal.class).build();
        return SearchUtl.searchDataList(dto.getPageNum(),dto.getPageSize(),beanSearcher.search(CertificateOrderResDTO.class,build));
    }

    /**
     * 取消订单
     * @param reqDTO
     */
    @PostMapping(value = "/cancelOrder")
    public void cancelOrder(@RequestBody CertificateOrderReqDTO reqDTO){
        orderService.cancelOrder(reqDTO);
    }

    /**
     * 支付
     * @param reqDTO
     * @return
     */
    @PostMapping(value = "/payOrder")
    public ScanPayReq payOrder(@RequestBody CertificateOrderReqDTO reqDTO, HttpServletRequest request) {

        return orderService.payOrder(reqDTO,request);
    }
    private final CertificateInfoService certificateInfoService;

    /**
     * 已有证书
     */
    @GetMapping(value = "/certificateInfoList")
    public List<CertificateInfoResDTO> certificateInfoList() {

        return certificateInfoService.memberCertificateInfo();
    }

    @GetMapping(value = "/logoutCertificate/{id}")
    public String logoutCertificate(@PathVariable Integer id){
        certificateInfoService.logoutCertificate(id);
        return Constants.SUCESS;
    }

}
